package com.example.web;

import com.example.entity.TUser;
import com.example.result.R;
import com.example.service.UserService;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping(value="/api/login/info")
    public R getLoginUserInfo(Authentication authentication) {
        TUser tUser = (TUser) authentication.getPrincipal();
        return R.OK(tUser);
    }

    @GetMapping(value = "/api/login/free")
    public R loginFree() {
        return R.OK();
    }

    @GetMapping(value="/api/users")
    public R loadUserInfo(@RequestParam(value="current", required = false) Integer current,
                          @RequestParam(value="pageSize", required = false) Integer pageSize) {
        if (current == null) {
            current = 1;
        }
        PageInfo<TUser> tUserPageInfo = userService.loadUserByPage(current, pageSize);
        return R.OK(tUserPageInfo);
    }

    @PutMapping(value = "/api/user/edit")
    public R editUserInfo(
            @RequestHeader(value="Authorization") String Authorization,
            @RequestBody TUser userInfo) {
        int row = userService.editUser(userInfo, Authorization);
        return row > 0 ? R.OK() : R.FAIL();
    }

    @PostMapping(value ="/api/user/add")
    public R addUserInfo(@RequestBody TUser tUser,
                         @RequestHeader(value="Authorization") String Authorization) {
        int row = userService.addUser(tUser, Authorization);
        return row > 0 ? R.OK() : R.FAIL();
    }

    @GetMapping(value="/api/user/{id}")
    public R getUserInfoById(@PathVariable(value="id") int id) {
        TUser tUser = userService.loadUserByUserId(id);
        return R.OK(tUser);
    }

    @DeleteMapping(value="/api/user/del/{id}")
    public R delUserInfoById(@PathVariable(value = "id") int id) {
        int row = userService.delUserInfoById(id);
        return row > 0 ? R.OK() : R.FAIL();
    }

    @DeleteMapping(value="/api/user/batchDel")
    public R batchDelUserInfo(@RequestParam("selVals") String selVarls){
        String[] selsArr = selVarls.split(",");
//        for (String sel : seleArr) {
//            userService.delUserInfoById(Integer.valueOf(sel));
//        }
        List<String> idList = Arrays.asList(selsArr);
        int row = userService.batchDelUserInfo(idList);
        return row >= idList.size() ? R.OK() : R.FAIL();
    }

    /**
     * 下载用户模板
     * @return
     */
    @GetMapping(value="/api/user/downloadUserTemplate")
    public R downloadUserTemplate(@RequestParam(value = "isBoolean") String isBoolean) {
        userService.downTemplate(isBoolean);
        return R.OK();
    }

    /**
     * 通过excel导入用户数据
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping(value="api/user/importUserExcel")
    public R importUserExcel(MultipartFile file) throws IOException {
//        注意：这里MultipartFile后的名字file，要与前端传递时的名字file相同
        System.out.println("file" + file);
        userService.uploadExcel(file.getInputStream());
        return R.OK();
    }
}
